//
//  StatusCell.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/8.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35
///微博cell
class StatusCell: UITableViewCell {
    /// 微博视图模型
    var viewModel:StatusViewModel? {
        didSet{
            
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            pictureView.viewModel = viewModel
            //配图行高
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
               
            }
        }
    }
    // MARK: - 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 根据指定的视图模型计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns: 返回视图模型对应的行高
    func rowHeight(vm:StatusViewModel)->CGFloat {
        // 1. 记录视图模型 -> 会调用上面的 didSet 设置内容以及更新`约束`
        viewModel = vm
        // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        // 3. 返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }
    // MARK: - 懒加载控件
    /// 顶部视图
    lazy var topView:StatusCellTopView = StatusCellTopView()
    ///微博正文标签
    lazy var contentLabel:UILabel = UILabel(Labeltext: "微博正文",  Labelfont: 15,screenInset: StatusCellMargin)
    //图片视图
    lazy var pictureView:StatuPictureView = StatuPictureView()
    ///底部视图
    lazy var bottomView:StatusCellBottomView = StatusCellBottomView()
    
}
// MARK: - 设置界面
extension StatusCell {
    func setUpUI() {
        let TopView = UIView()
        TopView.backgroundColor = UIColor.lightGrayColor()
        //1.添加控件
        contentView.addSubview(TopView)
        contentView.addSubview(topView)
        contentView.addSubview(pictureView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        // 2. 自动布局
        // 1> 顶部视图
        TopView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            make.height.equalTo(StatusCellMargin)
        }
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(TopView.snp_bottom)
            make.left.right.equalTo(contentView)
            make.height.equalTo(2*StatusCellMargin + StatusCellIconWidth)
        }
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
        //        // 3> 配图视图
        //        pictureView.snp_makeConstraints { (make) -> Void in
        //            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
        //            make.left.equalTo(contentLabel.snp_left)
        //            make.width.equalTo(300)
        //            make.height.equalTo(90)
        //        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.height.equalTo(44)
            
        }
    }
    
}
